package com.hhcn.friend;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2018/5/25.
 */
public class FriendReduce extends Reducer<Text, Text, Text, IntWritable> {

    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        Set<String> sets = new HashSet<String>();
        int i = 0;
        for (Text value : values) {
            context.write(pairKey(key.toString(), value.toString()), new IntWritable(1));
            for (String str : sets) {
                context.write(pairKey(str, value.toString()), new IntWritable(2));
            }
            sets.add(value.toString());
        }
    }

    private Text pairKey(String first, String second) {
        if (first.compareTo(second) < 0) {
            return new Text(first + "," + second);
        } else {
            return new Text(second + "," + first);
        }
    }

}
